use std::time::Duration;

use crate::config::app_config::CONFIG;
use sqlx::ConnectOptions;
use sqlx::Pool;
use sqlx::Sqlite;

 
pub struct DbConfig;

impl DbConfig {
    pub async fn sqlx_pool() -> anyhow::Result<Pool<Sqlite>> {
        let db_path = &CONFIG.database.sqlite.url;
        let sqlite_connect = sqlx::sqlite::SqliteConnectOptions::new()
            .journal_mode(sqlx::sqlite::SqliteJournalMode::Wal)
            .busy_timeout(Duration::from_secs(3))
            .synchronous(sqlx::sqlite::SqliteSynchronous::Off)
            .page_size(32768)
            .pragma("cache_size", "100000")
            .pragma("temp_store", "memory")
            .foreign_keys(true)
            .log_statements(log::LevelFilter::Warn)
            .filename(db_path.replace("sqlite://", ""));
        Ok(sqlx::sqlite::SqlitePoolOptions::new()
            .connect_with(sqlite_connect)
            .await?)
    }
}
